#include "loginwidget.h"
#include "ui_loginwidget.h"
#include "mainwidget.h"
#include "registwidget.h"
#include "myconfig.h"
#include "../MyTcp2/tcpsession.h"

LoginWidget::LoginWidget(TcpSession* session, QWidget *parent)
    : QWidget(parent), ui(new Ui::LoginWidget),session(session)
{
    ui->setupUi(this);
    this->setWindowTitle("登录");
    this->setWindowIcon(QIcon(":/img/login.svg"));
    //
    //设置密码显示框的内容显示加密
    hidePasswd();
    //创建tcp连接
    QObject::connect(session, &TcpSession::login_ret_signal, this, &LoginWidget::on_login_ret_signal);
}

//密码输入框显示密码
void LoginWidget::showPasswd()
{
    QPixmap pix(":/img/pwd-hidden.jpg");
    pix = pix.scaled(ui->label_pwd->size());
    ui->label_pwd->setPixmap(pix);
    ui->lineEdit_pwd->setEchoMode(QLineEdit::Normal);
}

//密码输入框隐藏密码
void LoginWidget::hidePasswd()
{
    QPixmap pix(":/img/pwd-show.jpg");
    pix = pix.scaled(ui->label_pwd->size());
    ui->label_pwd->setPixmap(pix);
    ui->lineEdit_pwd->setEchoMode(QLineEdit::Password);
}

//鼠标左键
void LoginWidget::mousePressEvent(QMouseEvent *event)
{
    QWidget * widget=childAt(event->pos());
    if(widget == nullptr)
        return;
    if(ui->label_pwd->objectName() != widget->objectName())
        return;
    if(event->button()!=Qt::LeftButton)
        return;
    showPasswd();
}

//鼠标右键
void LoginWidget::mouseReleaseEvent(QMouseEvent *event)
{
    QWidget * widget=childAt(event->pos());
    if(widget == nullptr)
        return;
    if(ui->label_pwd->objectName() != widget->objectName())
        return;
    if(event->button()!=Qt::LeftButton)
        return;
    hidePasswd();
}


//登录按钮- 点击事件
void LoginWidget::on_pushButton_login_clicked()
{
    //打包
    QString username=ui->lineEdit_username->text();
    QString passwd=ui->lineEdit_pwd->text();
    Pack pack;
    pack.setType(LOGIN);
    pack.setFrame(CALL);
    pack.append(username);
    pack.append(passwd);
    //发送数据
    session->send((char*)&pack, pack.getPacksize());
//    int cntSend=session->send((char*)&pack, pack.getPacksize());
//    qDebug() << "登录： cntSend=["<< cntSend<<"]已经发送";
}

//注册按钮-点击事件
void LoginWidget::on_pushButton_reg_clicked()
{
    if(registWidget == nullptr)
        registWidget = new RegistWidget(session, this);
    registWidget->show();
    this->hide();
}

//登录回复报文
//void LoginWidget::on_login_ret_signal(bool res, const QString &username, const QString &nickname)
//{
//    j++;
//    qDebug() << "第["<< j <<"]次收到登录回复报文:"<<res;
//    if(res==false){
//        QMessageBox::critical(this, "登录回复" , "登录失败"+username);
//        return;
//    }
//    QMessageBox::information(this, "登录回复", "登录成功");
//    TcpSession* session=dynamic_cast<TcpSession*>(sender());
//    //
//    if(mainWidget==nullptr){
//        mainWidget=new MainWidget(session, this, username, nickname);
//    }

//    mainWidget->show(session, this, username, nickname);
//    this->hide();
//}


void LoginWidget::on_login_ret_signal(bool res, const QString &username, const QString &nickname)
{
    if(res==false){
        QMessageBox::critical(this, "登录回复" , "登录失败"+username);
        return;
    }
    QMessageBox::information(this, "登录回复", "登录成功");
    TcpSession* session=dynamic_cast<TcpSession*>(sender());
    //
    if(mainWidget==nullptr){
        mainWidget=new MainWidget(session, this, username, nickname);
    }

    mainWidget->show(session, this, username, nickname);
    this->hide();
}

